package com.probiz.estore.order.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.probiz.estore.common.model.order.OrderDelivery;
import com.probiz.estore.common.model.order.OrderExpress;
import com.probiz.estore.common.model.order.OrderShipment;
import com.probiz.estore.common.model.system.CompanyInfo;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.order.dao.OrderExpressDao;
import com.probiz.estore.order.service.OrderExpressManager;
import com.probiz.estore.system.service.CompanyInfoManager;


/**
 * Manager implementation for OrderExpress, responsible for business processing, and communicate between web and persistence layer.
 */
public class OrderExpressManagerImpl extends GenericManagerImpl<OrderExpress> implements OrderExpressManager {

	private OrderExpressDao orderExpressDao = null;

	private CompanyInfoManager companyInfoManager = null;
	/**
	 * @param orderExpressDao
	 *            the orderExpressDao to set
	 */
	public void setOrderExpressDao(OrderExpressDao orderExpressDao) {
		this.orderExpressDao = orderExpressDao;
	}
	

	public void setCompanyInfoManager(CompanyInfoManager companyInfoManager) {
		this.companyInfoManager = companyInfoManager;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = orderExpressDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(OrderExpress entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(OrderExpress entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderExpressManager#getXmlFormatData(com.probiz.estore.common.model.order.OrderShipment, com.probiz.estore.common.model.order.OrderDelivery)
	 */
	public Document getXmlFormatData(OrderShipment orderShipment,OrderDelivery orderDelivery){
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("data");
		//-------收货人信息------
		Element ship_name = root.addElement("ship_name");
		Element ship_regionFullName = root.addElement("ship_regionFullName");
		Element ship_address = root.addElement("ship_address");
		Element ship_tel = root.addElement("ship_tel");
		Element ship_zip = root.addElement("ship_zip");

		ship_name.setText(StringUtils.defaultString(orderShipment.getShippingAddress().getFirstname()));
		ship_regionFullName.setText(StringUtils.defaultString(orderShipment.getShippingAddress().getRegionFullName()));
		ship_address.setText(StringUtils.defaultString(orderShipment.getShippingAddress().getAddress1()));		
		ship_tel.setText(StringUtils.defaultString(orderShipment.getShippingAddress().getPhoneNumber()));
		ship_zip.setText(StringUtils.defaultString(orderShipment.getShippingAddress().getPostalcode()));
		
		//-------订单、发货单信息------
		Element store_name = root.addElement("store_name");
		Element order_no = root.addElement("order_no");
		Element shipment_item_count = root.addElement("shipment_item_count");
		Element shipment_no = root.addElement("shipment_no");
		
		CompanyInfo companyInfo = companyInfoManager.getDefaultCompany();
		if(StringUtils.isNotBlank(companyInfo.getCompanyName())){
			store_name.setText(companyInfo.getCompanyName());
		}else{
			store_name.setText("");
		}
		order_no.setText(orderShipment.getSalesOrder().getOrderNo());
		shipment_item_count.setText(orderShipment.getItemCount().toString());
		shipment_no.setText(orderShipment.getDocumentNo());
		
		//-------发货人信息------
		Element dly_name = root.addElement("dly_name");
		Element dly_regionFullName = root.addElement("dly_regionFullName");
		Element dly_address = root.addElement("dly_address");
		Element dly_tel = root.addElement("dly_tel");
		Element dly_zip = root.addElement("dly_zip");
		Element dly_mobile = root.addElement("dly_mobile");
		dly_name.setText(StringUtils.defaultString(orderDelivery.getShipperName()));
		dly_regionFullName.setText(StringUtils.defaultString(orderDelivery.getRegionFullName()));
		dly_address.setText(StringUtils.defaultString(orderDelivery.getAddress()));
		dly_tel.setText(StringUtils.defaultString(orderDelivery.getTelephone()));
		dly_zip.setText(StringUtils.defaultString(orderDelivery.getZipCode()));
		dly_mobile.setText(StringUtils.defaultString(orderDelivery.getMobilephone()));
		
		//-------其他信息------
		Element date_year = root.addElement("date_year");
		Element date_moth = root.addElement("date_moth");
		Element date_day = root.addElement("date_day");
		Calendar cal = Calendar.getInstance();
		int date_y=cal.get(Calendar.YEAR);
		int date_m=cal.get(Calendar.MONTH)+1;
		int date_d=cal.get(Calendar.DAY_OF_MONTH);		
		date_year.setText(Integer.toString(date_y));
		date_moth.setText(Integer.toString(date_m));
		date_day.setText(Integer.toString(date_d));
		
		return document;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderExpressManager#listAvailableOrderExpress()
	 */
	public List<OrderExpress> listAvailableOrderExpress(){
		return orderExpressDao.listAvailableOrderExpress();
	}

}
